﻿using Basefr.Attributes;
using Basefr.Core.Entities;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Paas.Core.Documents.Entities
{
    /// <summary>
    /// 文档目录
    /// </summary>
    [MainTable(nameof(DocumentFolder))]
    public class DocumentFolder : EntityBase, ISelfReference<DocumentFolder>
    {
        /// <summary>
        /// 名称
        /// </summary>
        [Required]
        [MaxLength(500)]
        public string Name { get; set; }


        /// <summary>
        /// 上级目录
        /// </summary>
        public int? ParentId { get; set; }
        [ForeignKey(nameof(ParentId))]
        public DocumentFolder Parent { get; set; }

        public int Level { get; set; }

        public virtual List<DocumentFolder> Children { get; set; }


        [NotMapped]
        public string FullName
        {
            get
            {
                var names = new List<string>();

                PushName(this);

                void PushName(DocumentFolder item)
                {
                    names.Add(item.Name);

                    if (item.Parent != null)
                    {
                        PushName(item.Parent);
                    }
                }

                names.Reverse();

                return string.Join(" / ", names);
            }
        }



        [NotMapped]
        public List<int> Ids
        {
            get
            {
                var ids = new List<int>();

                PushId(this);

                void PushId(DocumentFolder item)
                {
                    ids.Add(item.Id);

                    if (item.Parent != null)
                    {
                        PushId(item.Parent);
                    }
                }

                ids.Reverse();

                return ids;
            }
        }

    }
}

